﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace SharingCenter.Web.Mod.Groups
{
    using SharingCenter.Web.Classes;
    using SharingCenter.Web.Classes.enums;
    using SharingCenter.Web.Classes.Groups;

    public partial class ThreadList : GroupsPageBase
    {
        protected Guid categoryId;

        protected void Page_Init(object sender, EventArgs e)
        {
            ParseGroupIdFromQueryString();
            HasRights(groupId, Profile.UserId);

            LinqDataSource1.Selecting += new EventHandler<LinqDataSourceSelectEventArgs>(LinqDataSource1_Selecting);
            ListView1.ItemDataBound += new EventHandler<ListViewItemEventArgs>(ListView1_ItemDataBound);
            btnSearch.Click += new EventHandler(btnSearch_Click);
            categoryId = GroupOperations.GetGroupThreadCategoryId(groupId);
            
        }

        private SiteMapNode SiteMap_SiteMapResolve(object sender, SiteMapResolveEventArgs e)
        {
            SiteMapNode currentNode = SiteMap.CurrentNode.Clone(true);
            SiteMapNode tmpNode = currentNode.ParentNode;
            tmpNode.Title = group.GroupName;
            tmpNode.Url += "?id=" + groupId;
            return currentNode;
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            SiteMap.SiteMapResolve += new SiteMapResolveEventHandler(this.SiteMap_SiteMapResolve);
            btnSearch.PostBackUrl = "~/Mod/Talks/Search.aspx?id=" + groupId;
        }

        protected void Page_Unload(object sender, EventArgs e)
        {
            SiteMap.SiteMapResolve -= new SiteMapResolveEventHandler(this.SiteMap_SiteMapResolve);
        }

        void btnSearch_Click(object sender, EventArgs e)
        {
            if (IsPostBack)
            {
                string query = string.Format("SearchPost '{0}', '{1}'", Request.Form["keyword"], Request.Form["category"]);
                List<ThreadWithMainPost> result = scDataContext.Instance.ExecuteQuery<ThreadWithMainPost>(query).ToList();
                ListView1.DataSource = result;
                ListView1.DataBind();
            }
        }

        void LinqDataSource1_Selecting(object sender, LinqDataSourceSelectEventArgs e)
        {
            using (scDataContext cxt = new scDataContext())
            {
                e.Result = cxt.GetGroupThreads(groupId, 0).ToList();
            }
        }

        void ListView1_ItemDataBound(object sender, ListViewItemEventArgs e)
        {
            //Sc_PostThread item = (Sc_PostThread)((ListViewDataItem)e.Item).DataItem;
            ListViewDataItem i = (ListViewDataItem)e.Item;
            Panel hotPanel = (Panel)e.Item.FindControl("pnHot");
            int hotByRead = int.Parse(SiteConfig.ReadSetting(SiteConfigSection.Post, "HotThreadByReadCount"));
            int hotByReply = int.Parse(SiteConfig.ReadSetting(SiteConfigSection.Post, "HotThreadByReplyCount"));

            hotPanel.Visible = Convert.ToBoolean(DataBinder.GetPropertyValue(i.DataItem, "IsHot")) || hotByRead > 0 && Convert.ToInt32(DataBinder.GetPropertyValue(i.DataItem, "ViewCount")) >= hotByRead || hotByReply > 0 && Convert.ToInt32(DataBinder.GetPropertyValue(i.DataItem, "TotalPosts")) - 1 >= hotByReply;
        }
    }
}
